<?php

class PikachuGamesController extends PikachuAppController{
	var $uses = array();

	protected $_denyActions = array('score');

	public function view(){
		// get top score to display
		$this->set('title_for_layout', 'Trò chơi - Chơi game Pikachu Online');
		Utils::loadModels($this, array('Pikachu.PikachuScore'));
		$this->PikachuScore->bindModels(array('belongsToPikachuPlayer'));
		$data = $this->PikachuScore->find('all', array(
			'fields' => array(
				'DISTINCT PikachuPlayer.id', 'PikachuPlayer.email' , 'PikachuPlayer.display_name',
				'PikachuScore.trusted', 'MAX(PikachuScore.score) as max_score'
			),
			'conditions' => array(),
			'limit' => 10,
			'order' => 'max_score DESC',
			'group' => 'PikachuPlayer.id'
		));
		foreach($data as &$d){
			$d['PikachuScore']['score'] = $d[0]['max_score'];
		}
		$this->set('scores' , $data);
	}

	public function ws($action){
		$params = $this->params['form'];
		$pMoves = isset($params['moves']) ? $params['moves'] : '';
		$token = isset($params['token']) ? $params['token'] : '';
		$moves = array();
		$pMoves = explode(';', $pMoves);
		foreach($pMoves as $p){
			if(empty($p)){
				continue;
			}
			$p = explode(',', $p);
			$i = array(
				'i1' => $p[0],
				'j1' => $p[1],
				'i2' => $p[2],
				'j2' => $p[3],
				'value' => $p[4],
				'timestamp' =>$p[5]
			);
			$moves[] = $i;
		}
		Utils::loadComponents($this, array('Pikachu.PikachuLogic'));
		$this->PikachuLogic->setData($this->Session->read('Pikachu'));
		switch($action){
			case 'startgame':
					$this->PikachuLogic->newGame();
				break;
			case 'youwin':
					$this->PikachuLogic->checkToken($token);
					$this->PikachuLogic->setMoves($moves);
					$this->PikachuLogic->endGame(true); // win = true
				break;
			case 'youloss':
					$this->PikachuLogic->checkToken($token);
					$this->PikachuLogic->setMoves($moves);
					$this->PikachuLogic->endGame(false); // win = false
				break;
			case 'endstage':
					$this->PikachuLogic->checkToken($token);
					$this->PikachuLogic->setMoves($moves);
					$this->PikachuLogic->endStage(true); // clear stage = true
				break;
			case 'newstage':
					$this->PikachuLogic->newStage();
			case 'pause':
					$this->PikachuLogic->checkToken($token);
					$this->PikachuLogic->setMoves($moves);
					$this->PikachuLogic->pause();
				break;
			case 'resume':
					$this->PikachuLogic->checkToken($token);
					$this->PikachuLogic->resume();
				break;
			case 'refresh':
					$this->PikachuLogic->checkToken($token);
					$this->PikachuLogic->setMoves($moves);
					$this->PikachuLogic->refresh();
				break;
		}
		$gameData = $this->PikachuLogic->getData();
		$this->Session->write('Pikachu', $gameData);
		$this->response['score'] = $gameData['score'];
		$this->response['scorea'] = $gameData['scorea']; // additional score
		$this->response['token'] = $gameData['token'];
		$this->response['game'] = $gameData;
		$this->sendAjax();
	}

	public function score(){
		Utils::loadComponents($this, array('Pikachu.PikachuLogic'));
		$player = $this->PikachuLogic->syncPlayer(
			array(
				'PikachuPlayer' => array(
					'email' => $this->Auth->user('email')
				)
			)
		);
		$this->PikachuLogic->setData($this->Session->read('Pikachu'));
		$this->PikachuLogic->saveScore($player);
		$this->redirect(array('action' => 'view'));
	}
}